﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace Gui.Web.Listado
{
    public partial class ErrorLista : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ObtenerDatos();
        }

        protected void NuevoError_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Forms/IngresoError.aspx", true);
        }

        protected void ListadoErrores_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var index = Convert.ToInt32(e.CommandArgument);
            ListadoErrores.SelectRow(index);
            var selectedDataKey = ((GridView)sender).SelectedDataKey;
            if (selectedDataKey == null) return;
            int idError;
            int.TryParse(selectedDataKey.Value.ToString(), out idError);

            switch (e.CommandName)
            {
                case "Editar":
                    Response.Redirect("../Forms/IngresoError.aspx?IdError=" + idError, true);
                    break;
                case "Eliminar":
                    {
                        var error = new ErroresBusiness();
                        error.ActivarInactivarError(idError);
                        ObtenerDatos();
                    }
                    break;
            }
        }

        private void ObtenerDatos()
        {
            var errores = new ErroresBusiness();
            ListadoErrores.DataSource = errores.ObtenerErrores(true).Errores;
            ListadoErrores.DataBind();
        }

    }
}